package com.cqjtu.store.admin.service.impl;

import com.cqjtu.store.admin.mapper.AdminMapper;
import com.cqjtu.store.admin.pojo.DTO.AdminLoginDTO;
import com.cqjtu.store.admin.pojo.VO.AdminLoginVO;
import com.cqjtu.store.admin.pojo.entity.Admin;
import com.cqjtu.store.admin.service.AdminService;
import com.cqjtu.store.common.*;
import com.cqjtu.store.user.pojo.dto.UserLoginDTO;
import com.cqjtu.store.user.pojo.entity.User;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Override
    public AdminLoginVO login(AdminLoginDTO adminLoginDTO) {
            //根据用户名去查询用户
            Admin admin = adminMapper.getUserByName(adminLoginDTO.getUsername());
            if(admin==null){
                //返回的是前端开发人员
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"管理员不存在");
            }
            //对密码进行加密后才判断
            if(!adminLoginDTO.getPassword().equals(admin.getPassword())){
                throw new ServiceException(ServiceCode.ERR_PWD,"密码错误");
            }
            //从user中取出需要的数据：id,username,封装
            AdminLoginVO adminLoginVO = new AdminLoginVO();
            adminLoginVO.setId(admin.getId());
            adminLoginVO.setUsername(admin.getUsername());
            //生成token
            String token = JwtUtils.generateToken(adminLoginVO);
            adminLoginVO.setToken(token);
            return adminLoginVO;


    }
}
